	<?php
	$cond = 'order BY refer_by_user_id desc';
$sort = '';
$ut = $u = $fn = $ln = $dg = 'asc';
if(isset($_GET['sort']))
{
  if($_GET['sort']=='ref_first_name')
  {
    if($_GET['s_type']=='asc')
    {	$cond=' ORDER BY u.first_name ASC';	$sort = '&sort=referhistory&s_type=asc';	$ut='desc';	}
    else
    {	$cond=' ORDER BY u.first_name DESC';	$sort = '&sort=referhistory&s_type=desc';	}
  }
  else if($_GET['sort']=='ref_by_first_name')
  {
    if($_GET['s_type']=='asc')
    {	$cond=' ORDER BY b.first_name ASC';	$sort = '&sort=referhistory&s_type=asc';	$ut='desc';	}
    else
    {	$cond=' ORDER BY b.first_name DESC';	$sort = '&sort=referhistory&s_type=desc';	}
  }
  else if($_GET['sort']=='refer_comm')
  {
    if($_GET['s_type']=='asc')
    {	$cond=' ORDER BY refer_comm ASC';	$sort = '&sort=referhistory&s_type=asc';	$ut='desc';	}
    else
    {	$cond=' ORDER BY refer_comm DESC';	$sort = '&sort=referhistory&s_type=desc';	}
  }
  else if($_GET['sort']=='buy_date')
  {
    if($_GET['s_type']=='asc')
    {	$cond=' ORDER BY buy_date ASC';	$sort = '&sort=referhistory&s_type=asc';	$ut='desc';	}
    else
    {	$cond=' ORDER BY buy_date DESC';	$sort = '&sort=referhistory&s_type=desc';	}
  }
}
	?>	  
<table width="100%"  border="1" cellspacing="0" class="container">
  <tr>
    <td width="469" height="25" class="tdHeader">Referred Commission History</td>
  </tr>
  <tr>
    <td height="577">
      <table width="100%">
            <tbody>
		<tr>
			<td>
				<form name="frmSearchPro" action="" method="post">
				<table width="100%" border="0">
					<tr>
					    <td width="20%">Referred User</td>
					    <td width="30%"><input type="text" name="ref_first_name" id="ref_first_name" style="width: 150px;" value="<?php echo $_POST['ref_first_name']?>" /></td>
					    <td width="20%">&nbsp;</td>
					    <td width="30%">&nbsp;</td>
					</tr>
					<tr>
					    <td width="20%">Referred By User</td>
					    <td width="30%"><input type="text" name="ref_by_first_name" id="ref_by_first_name" style="width: 150px;" value="<?php echo $_POST['ref_by_first_name']?>" /></td>
					    <td width="20%">Referred Commission</td>
					    <td width="30%"><input type="text" name="refer_comm" id="refer_comm" style="width: 150px;" value="<?php echo $_POST['refer_comm']?>" /></td>
					</tr>
					<tr>
					    <td width="20%">Paid</td>
					    <td width="30%">
					      <select name="isPaid" id="isPaid">
						<option value="">Select</option>
						<option value="0">No</option>
						<option value="1">Yes</option>
					      </select>
					      <script>$('#isPaid').val(<?php echo $_POST['isPaid']; ?>)</script>
					    </td>
					    <td width="20%">Date</td>
					    <td width="30%"><input type="text" name="buy_date" id="buy_date" style="width: 150px;" value="<?php echo $_POST['buy_date']?>" /></td>
					</tr>
					<tr>
						<td height="35" colspan="5" style="text-align:center"><input type="submit" name="btnSearchPro" value="Search" /></td>
					</tr>
				</table>
				</form>
			</td>
		</tr>
		<tr>
		  <td>
		      <b>Sort  By :</b>
		      <a href="<?php echo $base_url;?>admin/index.php?referhistory&sort=ref_first_name&s_type=<?php echo $ut;?>">Referred User</a> &nbsp;&nbsp;&nbsp;
		      <a href="<?php echo $base_url;?>admin/index.php?referhistory&sort=ref_by_first_name&s_type=<?php echo $ut;?>">Referred By User</a> &nbsp;&nbsp;&nbsp;
		      <a href="<?php echo $base_url;?>admin/index.php?referhistory&sort=refer_comm&s_type=<?php echo $ut;?>">Commission</a> &nbsp;&nbsp;&nbsp;
		      <a href="<?php echo $base_url;?>admin/index.php?referhistory&sort=buy_date&s_type=<?php echo $ut;?>">Date</a> &nbsp;&nbsp;&nbsp;
		  </td>
		</tr>
        </tbody></table>
	<br>
	<div style="overflow-x:scroll;">
	<table width="100%"  border="0" cellspacing="0">      
      <tr class="InnerHeader">    
	   <td width="5%" class="line">Sln no.</td>   
        <td width="12%" class="line">Product</td>
		<td width="22%" class="line">Referred.User(Type) </td>
        <td width="22%" class="line">ReferredBy.User(Type)</td>
		<?php /*?><td width="10%" class="line">Ref.User(Merchant)</td>
		<td width="20%" class="line">User Who Referred</td><?php */?>
		<td width="13%" class="line">Transcation Amount</td>
		<td width="13%" class="line">Referred Comm</td>	
		<td width="3%" class="line">Paid</td>	
		<td width="10%" class="line">Date</td>	
        </tr>
      
      <?php 
	  
	  

	  			ini_set('display_errors', 1); 
				ini_set('error_reporting', 'E_error'); 
				 require_once "../includes/pagination.php";
				 $itemPerPage=20;				 
				 $year=date("Y",time());
				 $month=date("m",time());				 
				 $total=0;
				if (isset($_GET['page']) && (int)$_GET['page'] > 0) 
				{
					$pageNumber = (int)$_GET['page'];		
				} 
				else 
				{
					$pageNumber = 1;		
				}
				
				$search=$search1='';
				if(isset($_POST['btnSearchPro'])) {
					
					$product_name		= $_POST['product_name'];		
					$ref_first_name		= $_POST['ref_first_name'];
					$ref_by_first_name 	= $_POST['ref_by_first_name'];
					$refer_comm		= $_POST['refer_comm'];
					$isPaid			= $_POST['isPaid'];
					
					if($product_name!='')
					$search1.= " AND product_name='".$product_name."'";
					
					if($ref_first_name!='')
					$search.=" AND u.first_name='".$ref_first_name."'";
					
					if($ref_by_first_name!='')
					$search.=" AND b.first_name='".$ref_by_first_name."'";
					
					if($refer_comm!='')
					$search.=" AND refer_comm='".$refer_comm."'";
					
					if($isPaid!='')
					$search.=" AND isPaid='".$isPaid."'";
					
				}
				$sql_shocat="SELECT  DATE_FORMAT(buy_date,GET_FORMAT(DATE,'USA')) as buy_date,isPaid,transaction_id,ref_comm_id,item_id,refer_comm,refer_to_user_id,refer_by_user_id,u.first_name as ref_first_name, u.last_name as ref_last_name,u.user_type as ref_user_type,b.first_name as ref_by_first_name, b.last_name as ref_by_last_name,b.user_type as ref_by_user_type FROM tbl_refer_comm r LEFT JOIN tbl_users u ON r.refer_to_user_id = u.user_id  left join tbl_users b ON r.refer_by_user_id = b.user_id  WHERE  buy_date < '".$year."-".$month."-01' $search $cond";//limit ".$pageNumber.", ".$itemPerPage."";				 
				
				 $rst_shocat=mysql_query(getPagingQuery($sql_shocat,$itemPerPage));
				 $numRows = mysql_num_rows($rst_shocat);
				 
				 $i=(int)(($itemPerPage*($pageNumber-1))+1);
				 
				 if($numRows>0) {				
					 while($row_shocat = mysql_fetch_array($rst_shocat)) 
					 {					
													
					if($row_shocat['ref_user_type']==1)
						$ref_type = "Buyer";
					else if($row_shocat['ref_user_type']==2)
						$ref_type = "Seller";		 
					else if($row_shocat['ref_user_type']==3)
						$ref_type = "Merchant";	
					else if($row_shocat['ref_user_type']==4)
						$ref_type = "Seller/ Buyer";	
						
					if($row_shocat['ref_by_user_type']==1)
						$ref_by_type = "Buyer";
					else if($row_shocat['ref_by_user_type']==2)
						$ref_by_type = "Seller";		 
					else if($row_shocat['ref_by_user_type']==3)
						$ref_by_type = "Merchant";	
					else if($row_shocat['ref_by_user_type']==4)
						$ref_by_type = "Seller/ Buyer";	
						
						$sqlSelItem="select p.* from tbl_items i left join tbl_product p  on i.product_id=p.product_id where item_id=".$row_shocat['item_id']."";
						$rstSelItem=mysql_query($sqlSelItem);
						$rowSelItem=mysql_fetch_array($rstSelItem);
						if(is_array($rowSelItem))
						{
							$product_name=$rowSelItem['product_name'];
						}
						
						$sqlSelItem="select * from tbl_orders where transaction_ID='".$row_shocat['transaction_id']."'";
						$rstSelItem=mysql_query($sqlSelItem);
						$rowSelItem=mysql_fetch_array($rstSelItem);
						
						if(is_array($rowSelItem))
						{
							$order_amt=$rowSelItem['order_amount'];
						}
						
						$total=$total+$row_shocat['refer_comm'];
						
						if($row_shocat['isPaid']==1)
						{
							$paid="Yes";
						}
						else
						{
							$paid="No";
						}
					 			 
				 ?> 
					<tr>
					<td class="line"><?php echo ($i);?></td>
					<td class="line"><?php echo $product_name;?></td>
					<td class="line"><?php echo $row_shocat['ref_first_name'].' '.$row_shocat['ref_last_name'];?>(<?php echo $ref_type;?>)</td>
					<td class="line"><?php echo $row_shocat['ref_by_first_name'].' '.$row_shocat['ref_by_last_name'];?>(<?php echo $ref_by_type;?>)</td>					
					<td class="line">$<?php echo $order_amt;?></td>
					<td class="line">$<?php echo $row_shocat['refer_comm'];?></td>			
					<td class="line"><?php echo $paid;?></td>
					<td class="line"><?php echo $row_shocat['buy_date']?></td>
      				</tr><?php $i++; } }?>     
       </table>
	<div align="center">
		<?php		   
		   $link="referhistory";
		   echo $disp=getPagingLink($sql_shocat,$itemPerPage,$link);							   
		?>    
	</div>
	</div>
	</td>
 </tr>
 <tr>
 <td style="text-align:right;"></td>
 </tr>
</table>